package com.zzh.structuredeployment.activity

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.zzh.bridge.page.PageApp.ELEVEN
import com.zzh.bridge_annotation.RouteTag
import com.zzh.structuredeployment.R
import com.zzh.structuredeployment.frag.ReleaseFragment
import com.zzh.structuredeployment.frag.SwatchesFragment
import com.zzh.tools.utils.ToolLog
import kotlinx.android.synthetic.main.activity_eleven.*
import kotlin.math.abs

/**
 * @author zhangzhihao
 * @email zhangzhihao@gongjiangren.net
 * @date 2021-06-01
 * @desc
 */
@RouteTag(value = ELEVEN)
class ElevenActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_eleven)

        setClick()
        createFragments()
        configViewPager()
    }

    private fun configViewPager() {
//        viewPager.offscreenPageLimit = 4
        viewPager.setPageTransformer(true, MyPageTransformer())
        viewPager.adapter = MyAdapter(supportFragmentManager)
        val adapter = viewPager.adapter as MyAdapter
        adapter.fragments = mFragments
        adapter.notifyDataSetChanged()
    }

    private val mFragments = arrayListOf<Fragment>()

    private fun createFragments() {
        mFragments.clear()
        for (i in 1..4) {
            mFragments.add(createFragment(i))
        }
    }

    private fun createFragment(i: Int): Fragment {
        return SwatchesFragment.newInstance(if (i % 2 > 0) "#ff00ff" else "#ff0000")
    }

    private fun setClick() {
        titleView.setOnClickListener {
            finish()
        }
        viewPager.addOnAdapterChangeListener { viewPager, oldAdapter, newAdapter ->
            ToolLog.i("zzh", ">>执行更换了adapter>>>>>>")
        }
        viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
                ToolLog.i("zzh", ">onPageScrollStateChanged>>>>${state}")
            }

            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
                ToolLog.i("zzh", ">onPageScrolled>>>>${position}")
            }

            override fun onPageSelected(position: Int) {
                ToolLog.i("zzh", ">onPageSelected>>>>${position}")
            }
        })

    }

    class MyAdapter(fragmentManager: FragmentManager) :
        FragmentPagerAdapter(fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
        var fragments: ArrayList<Fragment>? = null

        override fun getItem(position: Int): Fragment {
            return fragments!![position]
        }

        override fun getCount(): Int {
            return fragments?.size ?: 0
        }
    }

    class MyPageTransformer : ViewPager.PageTransformer {
        private val MIN_SCALE = 0.65f
        override fun transformPage(view: View, position: Float) {
            val pageWidth = view.width

            /**
             * position：这个position不是手指滑动的坐标位置，而是滑动页面相对于手机屏幕的位置，
             * 范围位：[-1,1],[-1,0)表示页面向左滑出屏幕，0表示处于中心（即当前显示）的页面，
             * (0,1]表示页面向右滑出屏幕
             */
            when {
                position < -1 -> { //表示已经滑出屏幕（左边）
                    view.alpha = 0f
                }
                position <= 0 -> { // [-1,0]
                    view.alpha = 1f
                    view.translationX = 0f
                    view.scaleX = 1f
                    view.scaleY = 1f
                }
                position <= 1 -> { // (0,1]
                    view.alpha = 1f - position

                    view.translationX = pageWidth * -position

                    var scaleFactor = MIN_SCALE + (1 - MIN_SCALE) * (1 - abs(position))
                    ToolLog.i("zzh", ">>scaleFactor>${scaleFactor}>>>>>")
                    view.scaleX = scaleFactor
                    view.scaleY = scaleFactor
                }
                else -> { //表示已经滑出屏幕（右边）
                    view.alpha = 0f
                }
            }
            ToolLog.i("zzh", "tag>>>position>${position}>>")
        }
    }

    class VerticalPageTransformer : ViewPager.PageTransformer {

        override fun transformPage(view: View, position: Float) {
            when {
                position < -1 -> { // [-Infinity,-1)
                    // This page is way off-screen to the left.
                    view.alpha = 0f
                }
                position <= 1 -> { // [-1,1]
                    view.alpha = 1f
                    // Counteract the default slide transition
                    view.translationX = view.width * -position;
                    //set Y position to swipe in from top
                    val yPosition = position * view.height;
                    view.translationY = yPosition;

                }
                else -> { // (1,+Infinity]
                    // This page is way off-screen to the right.
                    view.alpha = 0f
                }
            }

        }
    }
}